import collections
class Solution(object):
    def rearrangeBarcodes(self, barcodes):
        d = collections.Counter(barcodes)
        c = [[d[key], key] for key in d]
        c.sort(reverse=True)
        m = c[0][0]
        out = [[c[0][1]] for _ in range(m)]
        index = 0
        for count, t in c[1:]:
            while count > 0:
                out[index % m].append(t)
                count -= 1
                index += 1
        ans = []
        for temp in out:
            ans += temp
        return ans
